<template>
  <el-dialog
    title="设置默认头像"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="头像" prop="headPhoto">
        <!-- <el-input v-model="dataForm.headPhoto" placeholder="封面"></el-input> -->
        <el-upload
        class="avatar-uploader"
        :action="url"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Vue from 'vue'
export default {
  data() {
    return {
      dialogImageUrlss: '', //头像
      dialogVisibless: false,
      url:'',
      imageUrl: '',
      visible: false,
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      imagesList:[],
      dataForm: {
        id: "",
        title: "",
        zj: "",
        dj: "",
        images: "",
        sl: "",
        headPhoto: "",
        status: "",
      },
      options: [
        {
          value: 0,
          label: "上架",
        },
        {
          value: 1,
          label: "下架",
        },
      ],
      dataRule: {
        sl: [{ required: true, message: "数量不能为空", trigger: "blur" }],
        headPhoto: [
          { required: true, message: "头像不能为空", trigger: "blur" },
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    inputChangezj(data) {
      var min = 1
      if (Number(this.dataForm.zj.replace(/[^\d]/g, '')) < min) {
        this.dataForm.zj = min
      } else {
        this.dataForm.zj = this.dataForm.zj.replace(/[^\d]/g, '')
      }
    },
    inputChangedj(data) {
      var min = 1
      if (Number(this.dataForm.dj.replace(/[^\d]/g, '')) < min) {
        this.dataForm.dj = min
      } else {
        this.dataForm.dj = this.dataForm.dj.replace(/[^\d]/g, '')
      }
    },
    handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        this.dataForm.headPhoto = res.url
      },
      beforeAvatarUpload(file) {
        const isLt2M = file.size / 1024 / 1024 < 5;
       
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 5MB!');
        }
        return isLt2M;
      },
      handleAvatarSuccesses(res, file) {
        this.imagesList.push(res.url)
      },
      handleRemovess(file, fileList) {
        console.log(file, fileList);
      },
      handlePictureCardPreviewss(file) {
        this.dialogImageUrlss = file.url;
        this.dialogVisibless = true;
      },
    init(id) {
      this.url = this.$http.adornUrl(`app/oss/upload?token=${this.$cookie.get('token')}`)
      this.dataForm.id = id || "";
      this.visible = true;
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`yh/user/updateTx`),
              method: "post",
              params: this.$http.adornParams({
              userId: this.dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          } else {
          }
        }
      });
    },
  },
};
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>